/*
 * drivers/display/lcd/lcd_extern/mipi_P070ACB.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the named License,
 * or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include <malloc.h>
#include <asm/arch/gpio.h>
#ifdef CONFIG_OF_LIBFDT
#include <libfdt.h>
#endif
#include <amlogic/aml_lcd.h>
#include <amlogic/aml_lcd_extern.h>
#include "lcd_extern.h"

#define LCD_EXTERN_NAME			"mipi_P070ACB"
#define LCD_EXTERN_TYPE			LCD_EXTERN_MIPI

/******************** mipi command ********************/
/* format:  data_type, cmd_size, data.... */
/* 	data_type=0xff,
 *		0 < cmd_size < 0xff means delay ms,
 *		cmd_size=0 or 0xff means ending.
 *	data_type=0xf0, for gpio control
 *		data0=gpio_index, data1=gpio_value.
 *		data0=gpio_index, data1=gpio_value, data2=delay.
 *	data_type=0xfd, for delay ms
 *		data0=delay, data_1=delay, ..., data_n=delay.
 */
//******************************************************//
static unsigned char mipi_init_on_table[] = {
	0xfd, 1, 100,   /* delay */
	0x15, 2, 0x62, 0x01,
	0x29, 5, 0xFF, 0xAA, 0x55, 0x25, 0x01,
	0x23, 2, 0xFC, 0x08,
	0xfd, 1, 1,	   /* delay(ms) */
	0x23, 2, 0xFC, 0x00,

	0xfd, 1, 1,	   /* delay(ms) */
	0x23, 2, 0x6F, 0x21,
	0x23, 2, 0xF7, 0x01,
	0xfd, 1, 1,	   /* delay(ms) */
	0x23, 2, 0x6F, 0x21,
	0x23, 2, 0xF7, 0x00,
	0xfd, 1, 1,	   /* delay(ms) */

	0x23, 2, 0x6F, 0x1A,
	0x23, 2, 0xF7, 0x05,
	0xfd, 1, 1,	   /* delay(ms) */

	0x29, 5, 0xFF, 0xAA, 0x55, 0x25, 0x00,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x00,
	0x29, 3, 0xB1, 0x68, 0x41,
	0x23, 2, 0xB5, 0x88,
	0x29, 6, 0xBD, 0x02, 0xB0, 0x0C, 0x14, 0x00,
	0x23, 2, 0xC8, 0x80,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01,
	0x29, 3, 0xB3, 0x2D, 0x2D,
	0x29, 3, 0xB4, 0x19, 0x19,
	0x23, 2, 0xB5, 0x06,

	0x29, 3, 0xB9, 0x36, 0x36,
	0x29, 3, 0xBA, 0x26, 0x26,
	0x29, 3, 0xBC, 0xA8, 0x01,
	0x29, 3, 0xBD, 0xAB, 0x01,
	0x23, 2, 0xC0, 0x0C,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x02,
	0x23, 2, 0xEE, 0x02,
	0x29, 7, 0xB0, 0x00, 0x50, 0x00, 0x52, 0x00, 0x73,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xB0, 0x00, 0x8F, 0x00, 0xA5, 0x00, 0xCA,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xB0, 0x00, 0xEA, 0x01, 0x1B,
	0x29, 7, 0xB1, 0x01, 0x42, 0x01, 0x82, 0x01, 0xB3,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xB1, 0x02, 0x00, 0x02, 0x41, 0x02, 0x42,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xB1, 0x02, 0x78, 0x02, 0xB5,
	0x29, 7, 0xB2, 0x02, 0xDA, 0x03, 0x12, 0x03, 0x3A,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xB2, 0x03, 0x6E, 0x03, 0x8D, 0x03, 0xB1,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xB2, 0x03, 0xCA, 0x03, 0xE8,
	0x29, 5, 0xB3, 0x03, 0xF4, 0x03, 0xFF,

	0x29, 7, 0xBC, 0x00, 0x05, 0x00, 0x52, 0x00, 0x73,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xBC, 0x00, 0x8F, 0x00, 0xA5, 0x00, 0xCA,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xBC, 0x00, 0xEA, 0x01, 0x1B,
	0x29, 7, 0xBD, 0x01, 0x42, 0x01, 0x82, 0x01, 0xB3,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xBD, 0x02, 0x00, 0x02, 0x41, 0x02, 0x42,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xBD, 0x02, 0x78, 0x02, 0xB5,
	0x29, 7, 0xBE, 0x02, 0xDA, 0x03, 0x12, 0x03, 0x3A,
	0x23, 2, 0x6F, 0x06,
	0x29, 7, 0xBE, 0x03, 0x6E, 0x03, 0x8D, 0x03, 0xB1,
	0x23, 2, 0x6F, 0x0C,
	0x29, 5, 0xBE, 0x03, 0xCA, 0x03, 0xE8,
	0x29, 5, 0xBF, 0x03, 0xF4, 0x03, 0xFF,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x03,
	0x29, 6, 0xB2, 0x05, 0x00, 0x00, 0x00, 0x00,
	0x29, 6, 0xB6, 0x05, 0x00, 0x00, 0x00, 0x00,
	0x29, 6, 0xB7, 0x05, 0x00, 0x00, 0x00, 0x00,
	0x29, 6, 0xBA, 0x57, 0x00, 0x00, 0x00, 0x00,
	0x29, 6, 0xBB, 0x57, 0x00, 0x00, 0x00, 0x00,
	0x29, 5, 0xC0, 0x00, 0x34, 0x00, 0x00,
	0x29, 5, 0xC1, 0x00, 0x00, 0x34, 0x00,
	0x23, 2, 0xC4, 0x40,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x05,
	0x29, 3, 0xB0, 0x17, 0x06,
	0x29, 3, 0xB1, 0x17, 0x06,
	0x29, 3, 0xB2, 0x17, 0x06,
	0x29, 3, 0xB3, 0x17, 0x06,
	0x29, 3, 0xB4, 0x17, 0x06,

	0x29,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	0x29, 3, 0xCB, 0x34, 0x34,
	0x29, 3, 0xCC, 0x34, 0x2D,
	0x29, 3, 0xCD, 0x2D, 0x34,
	0x29, 3, 0xCE, 0x2D, 0x2D,
	0x29, 3, 0xCF, 0x34, 0x34,

	0x29, 3, 0xD0, 0x34, 0x34,
	0x29, 3, 0xD1, 0x09, 0x13,
	0x29, 3, 0xD2, 0x11, 0x1B,
	0x29, 3, 0xD3, 0x19, 0x17,
	0x29, 3, 0xD4, 0x15, 0x34,
	0x29, 3, 0xD5, 0x29, 0x2A,
	0x29, 3, 0xD6, 0x01, 0x2D,
	0x29, 3, 0xD7, 0x2D, 0x0B,
	0x29, 6, 0xD8, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x29, 6, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00,

	0x29, 3, 0xE5, 0x34, 0x34,
	0x29, 3, 0xE6, 0x34, 0x34,
	0x23, 2, 0xE7, 0x00,
	0x29, 3, 0xE8, 0x34, 0x34,
	0x29, 3, 0xE9, 0x34, 0x34,
	0x23, 2, 0xEA, 0x00,

	0x29, 6, 0xF0, 0x55, 0xAA, 0x52, 0x00, 0x00,

	0x13, 1, 0x35,
	0x13, 1, 0x11,
	0xfd, 1, 120,	   /* delay(ms) */
	0x13, 1, 0x29,
	0xfd, 1, 20,	   /* delay(ms) */
	0xff, 0,         /* ending */

};

static unsigned char mipi_init_off_table[] = {
	0x05, 1, 0x28, /* display off */
	0xfd, 1, 10,   /* delay 10ms */
	0x05, 1, 0x10, /* sleep in */
	0xfd, 1, 150,  /* delay 150ms */
	0xff, 0,   /* ending */
};

static int lcd_extern_driver_update(struct aml_lcd_extern_driver_s *ext_drv)
{
	if (ext_drv == NULL) {
		EXTERR("%s driver is null\n", LCD_EXTERN_NAME);
		return -1;
	}

	ext_drv->config->cmd_size = LCD_EXT_CMD_SIZE_DYNAMIC;
	ext_drv->config->table_init_on  = &mipi_init_on_table[0];
	ext_drv->config->table_init_on_cnt  = sizeof(mipi_init_on_table);
	ext_drv->config->table_init_off = &mipi_init_off_table[0];
	ext_drv->config->table_init_off_cnt  = sizeof(mipi_init_off_table);

	return 0;
}

int aml_lcd_extern_mipi_p070acb_probe(struct aml_lcd_extern_driver_s *ext_drv)
{
	int ret = 0;

	ret = lcd_extern_driver_update(ext_drv);

	if (lcd_debug_print_flag)
		EXTPR("%s: %d\n", __func__, ret);
	return ret;
}
